import request from "@/lib/utils/request";
import { DictType, DictData } from "./dict.d";
import { PaginationType, ResponseWithPagination, Response } from "../common";

export const getDictTypesApi = (
  params: PaginationType,
): ResponseWithPagination<DictType> => {
  return request.get("/system/dict/type/list", {
    params,
  });
};

export const addDictTypeApi = (data: DictType): Response<null> => {
  return request.post("/system/dict/type", data);
};

export const updateDictTypeApi = (data: DictType): Response<null> => {
  return request.put("/system/dict/type", data);
};

export const deleteDictTypeApi = (dictId: number): Response<null> => {
  return request.delete(`/system/dict/type/${dictId}`);
};

export const getDictTypeApi = (dictId: string): Response<DictType> => {
  return request.get(`/system/dict/type/${dictId}`);
};

export const getDictDataListApi = (
  params: PaginationType,
): ResponseWithPagination<DictData> => {
  return request.get(`system/dict/data/list`, { params });
};

export const addDictDataApi = (data: DictData): Response<null> => {
  return request.post("/system/dict/data", data);
};

export const updateDictDataApi = (data: DictData): Response<null> => {
  return request.put("/system/dict/data", data);
};

export const deleteDictDataApi = (dictId: number): Response<null> => {
  return request.delete(`/system/dict/data/${dictId}`);
};

export const getDictDataApi = (dictId: string): Response<DictData> => {
  return request.get(`/system/dict/data/${dictId}`);
};

export const getDictDatasByTypeApi = (type: string): Response<DictData[]> => {
  return request.get(`/system/dict/data/type/${type}`);
};

export const getDictDatasByTypesApi = async (
  types: string[],
): Promise<DictData[][]> => {
  const res = await Promise.all(types.map(getDictDatasByTypeApi));
  return res.map((item) => item.data);
};
